package cn.daniellee.plugin.command;

import cn.daniellee.plugin.PokeEditFilter;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.util.List;

public class BasicCommand implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (sender.isOp() || sender.hasPermission("pokeeditfilter.command")) {
			if (args.length > 0) {
				if ("add".equalsIgnoreCase(args[0]) && args.length > 1) {
					List<String> list = PokeEditFilter.config.getStringList("list");
					list.add(args[1]);
					PokeEditFilter.config.set("list", list);
					PokeEditFilter.getInstance().saveConfig();
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("add-success")).replace("&", "§"));
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("current-list") + "： " + StringUtils.join(list.toArray(new String[0]), ", ")).replace("&", "§"));
					return true;
				} else if ("remove".equalsIgnoreCase(args[0]) && args.length > 1) {
					List<String> list = PokeEditFilter.config.getStringList("list");
					if (list == null || list.isEmpty()) {
						sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("list-empty")).replace("&", "§"));
						return true;
					}
					for (int i = 0; i < list.size();) {
						if (list.get(i).equals(args[1])) list.remove(i);
						else i++;
					}
					PokeEditFilter.config.set("list", list);
					PokeEditFilter.getInstance().saveConfig();
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("remove-success")).replace("&", "§"));
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("current-list") + "： " + StringUtils.join(list.toArray(new String[0]), ", ")).replace("&", "§"));
					return true;
				} else if ("list".equalsIgnoreCase(args[0])) {
					List<String> list = PokeEditFilter.config.getStringList("list");
					if (list == null || list.isEmpty()) {
						sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("list-empty")).replace("&", "§"));
						return true;
					}
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("current-list") + "： " + StringUtils.join(list.toArray(new String[0]), ", ")).replace("&", "§"));
					return true;
				} else if ("reload".equalsIgnoreCase(args[0])) {
					PokeEditFilter.getInstance().reloadConfig();
					PokeEditFilter.config = PokeEditFilter.getInstance().getConfig();
					PokeEditFilter.prefix = "&7[&b" + PokeEditFilter.config.get("prompt-prefix") + "&7] &3: &r";
					sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("reload-success")).replace("&", "§"));
					return true;
				}
			}
			sendHelp(sender);
		} else sender.sendMessage((PokeEditFilter.prefix + PokeEditFilter.config.get("no-permission")).replace("&", "§"));
		return true;
	}

	private void sendHelp(CommandSender sender) {
		sender.sendMessage(("&m&a---&m&6--------&3 : " + PokeEditFilter.prefix + "&m&a--------&m&6---").replace("&", "§"));

		TextComponent addHelp = new TextComponent("/pef add <name>  " + PokeEditFilter.config.get("add-help"));
		addHelp.setColor(ChatColor.GRAY);
		addHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pef add "));
		addHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PokeEditFilter.config.get("add-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(addHelp);

		TextComponent removeHelp = new TextComponent("/pef remove <name>  " + PokeEditFilter.config.get("remove-help"));
		removeHelp.setColor(ChatColor.GRAY);
		removeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pef remove "));
		removeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PokeEditFilter.config.get("remove-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(removeHelp);

		TextComponent listHelp = new TextComponent("/pef list  " + PokeEditFilter.config.get("list-help"));
		listHelp.setColor(ChatColor.GRAY);
		listHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pef list"));
		listHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PokeEditFilter.config.get("list-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(listHelp);

		TextComponent reloadHelp = new TextComponent("/pef reload  " + PokeEditFilter.config.get("reload-help"));
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pef reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) PokeEditFilter.config.get("reload-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(reloadHelp);
	}
	
}
